package com.examination.client.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpMethod;

import static com.examination.client.util.HttpUtil.httpsRequest;

public class TengXunUtils {
    private static LogUtil logger = new LogUtil(OpenIdUtils.class);
    /**
     * 将高德地图经纬度转换成腾讯的
     */
    private static final String GAODE_TO_TENGXUN_URL = "https://apis.map.qq.com/ws/coord/v1/translate?locations=stationsLatLng&type=5&key=53KBZ-FM7YI-6W4GT-53FOU-ATGVE-4HFXV";

    public static String setGaodeToTengxun(String stations) {
        String[] splits = stations.split(";");
        StringBuffer stationsLatLng = new StringBuffer();
        for (int i = 0; i<splits.length; i++) {
            String[] split1 = splits[i].split(",");
            stationsLatLng.append(split1[1]);
            stationsLatLng.append(",");
            stationsLatLng.append(split1[0]);
            if(i < splits.length - 1){
                stationsLatLng.append(";");
            }
        }
        String replaceUrl = GAODE_TO_TENGXUN_URL.replace("stationsLatLng", stationsLatLng);
        logger.info(replaceUrl);
        JSONObject jsonObject = JSON.parseObject(httpsRequest(replaceUrl, HttpMethod.GET.name(), null));
        JSONArray jsonArray = jsonObject.getJSONArray("locations");
        String tengXunJsonArrayString = jsonArray.toJSONString();
        tengXunJsonArrayString.replace("lng", "longitude").replace("lat", "latitude");
        return tengXunJsonArrayString;
    }
}
